package com.shuogg.transaction.service.impl;

import com.shuogg.transaction.dao.UserDao;
import com.shuogg.transaction.model.User;
import com.shuogg.transaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public long add(User user) {
        return userDao.add(user);
    }

    @Override
    @Transactional
    public void update(User user) {

    }

    @Override
    @Transactional
    public void delete(Long id) {

    }

    @Override
    @Transactional
    public List<User> listAll() {
        return userDao.listAll();
    }

    @Override
    @Transactional
    public Long count() {
        return null;
    }

    @Override
    public Long countBy(String field, Object value) {
        return null;
    }

    @Override
    @Transactional
    public void deleteBy(String field, Object value) {

    }

    @Override
    @Transactional
    public User get(Long id) {
        return null;
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> idList) {
    }

    @Override
    @Transactional
    public User getBy(String field, Object value) {
        return null;
    }

    @Override
    @Transactional
    public User getByAnd(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    @Transactional
    public User getByOr(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    @Transactional
    public List<User> listBy(String field, Object value) {
        return userDao.listBy(field, value);
    }

    @Override
    @Transactional
    public List<User> listByAnd(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    @Transactional
    public List<User> listByOr(String field1, Object value1, String field2, Object value2) {
        return null;
    }

    @Override
    @Transactional
    public Long addTest() {
        User user1 = new User("小红帽", 88);
        User user2 = new User("大飞狼", 22);
        userDao.add(user1);
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userDao.add(user2);
    }
    /*
    * 关于@Transactional:
    *       这种注解必须跨层，才能经过动态代理，注解才会生效。异步注解也一样，同一个类下调用使用了注解的方法，代理没生效，换一个类写这个方法即可
    * 参考: [消失的事务](https://zhuanlan.zhihu.com/p/38208248)
    * */

    /* 测试事务嵌套问题: 结论->事务无法嵌套,只认最上层 */
    @Transactional
    public void innerTransaction() {
        userDao.add(new User("智乃",13));
        throw new RuntimeException();
    }

    public void noTransaction() {
        userDao.add(new User("智乃",13));
        throw new RuntimeException();
    }

    @Override
    public void doInnerTransaction() {
        userDao.add(new User("心爱",16));
        innerTransaction();
    }

    @Override
    public void doNoTransaction() {
        userDao.add(new User("心爱",16));
        noTransaction();
    }

    @Override
    @Transactional
    public void tdoInnerTransaction() {
        userDao.add(new User("心爱",16));
        innerTransaction();
    }
}
